<?php if (!defined('FIT_PATH')) exit('No direct script access allowed');
/**
 * @copyright 2009 - 2011 Bincent.com. All Rights Reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @package FitPHP
 * @subpackage core
 *
 * @author Hongbin Hsu <hongbin.hsu@gmail.com> Initial.
 * @version $Id:$
 */
 
// ------------------------------------------------------------------------

class Loader
{
	//----------------------------------------------------------
	/**
	 * 加载数据库操作类
	 *
	 * @param string $db
	 */
	public function database($db = 'default')
	{
		$database_path = FIT_PATH.'database'.DIRECTORY_SEPARATOR;
		require_once($database_path.'database.php');
		
		return Database::get_instance($db);
	}
	
	//----------------------------------------------------------
	/**
	 * 引用模板文件
	 */
	public function view($tpl = null, $data = array())
	{
		try {
			if (empty($tpl)) {
				$tpl = Router::get_module().DIRECTORY_SEPARATOR;
				$tpl.= Router::get_controller().DIRECTORY_SEPARATOR;
				$tpl.= Router::get_method().'.php';
				$tpl = ltrim($tpl, DIRECTORY_SEPARATOR);
			}
        	$filename = APP_PATH.'view'.DIRECTORY_SEPARATOR.$tpl;
        	if (file_exists($filename)) {
        		@extract($data, EXTR_SKIP);
				include_once($filename);
        	}else{
        		throw new Exception("Not found {$tpl} template");
        	}
        }catch (Exception $e){
        	$e->getMessage();
        }
	}
	
	//----------------------------------------------------------
	/**
	 * 实例化业务模型
	 *
	 * @param string $model 模型名称
	 * @param array $param 参数
	 * @param string $alias 别名
	 * @return void
	 */
	public function model($model, $alias = null, $param = array()){
		try 
		{
			if (empty($model)) {return false;}
			if (is_null($alias)) { $alias = $model; }
			
			$filename = APP_PATH.'model'.DIRECTORY_SEPARATOR;
			$filename.= strtolower($model).'.php';
			
			if (!file_exists($filename))
			{
				throw new Exception("Not found {$filename} file.");
			}
			
			include_once($filename);
			
			$object = $model.'_Model';
			$this->$alias = new $object(implode(',', $param));
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
	
	//----------------------------------------------------------
	/**
	 * 加载扩展类库
	 *
	 * @param string $extends	扩展类库名称
	 * @return void
	 */
	public function extend($extends = '')
	{
		if (!is_array($extends)) { $extends = array($extends); }
		
		foreach ($extends as $extend)
		{
			$filename = 'extend/'.$extend;
			$this->extend = autoload($filename, true);
		}
	}
	
	//----------------------------------------------------------
	/**
	 * 加载助手函数
	 *
	 * @param string $helers 助手文件名称
	 * @return void
	 */
	public function helper($helpers = array())
	{
		if (!is_array($helpers)) { $helpers = array($helpers); }
		
		foreach ($helpers as $helper)
		{
			$filename = 'helper/'.$helper;
			autoload($filename, false);
		}
	}
	
	//----------------------------------------------------------
	/**
	 * 加载功能类库
	 *
	 * @param string $librarys	功能类库文件名称
	 * @return void
	 */
	public function library($librarys = array())
	{
		if (!is_array($librarys)) { $librarys = array($librarys); }
		
		foreach ($librarys as $library)
		{
			$filename = 'library/'.$library;
			$this->$library = autoload($filename, true);
		}
	}
}